
package
{
	
	import flash.display.BitmapData;
	import flash.display.JPEGEncoderOptions;
	import flash.display.PNGEncoderOptions;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Matrix;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.ByteArray;
	
	import so.cuo.platform.wechat.WeChat;
	
	public class demo extends Sprite
	{
		private var wx:WeChat=WeChat.getInstance();
		public function demo()
		{
			super();
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			var ui:UISprite=new UISprite(clickHandler);
			this.addChild(ui);
			ui.addButton("check",0,0);
			ui.addButton("open",200,0);
			ui.addButton("sendTxt",0,160);
			ui.addButton("sendImage",280,180);
			ui.addButton("sendLink",280,280);
			if(!wx.supportDevice){
				trace("not support device");
				return;
			}else{
				wx.registerApp("wx21fb4d35f5eba36b");
			}
		}
		private function clickHandler(label:String):void{
			trace("click button",label);
			if(!wx.supportDevice){
				trace("not  support device");
				return;
			}
			if(label=="check"){
				trace(wx.getApiVersion(),wx.getWXAppInstallUrl(),wx.isWXAppInstalled(),wx.isWXAppSupportApi());
			}
			if(label=="open"){
				trace(wx.openWXApp());
			}
			if(label=="sendTxt"){
				trace(wx.sendTextMessage(new Date().toString(),WeChat.WXSceneTimeline));
			}
			if(label=="sendLink"){
				var url:String=this.saveInfoToCameraRoll(333,"3355");
				trace(wx.sendLinkMessage(url,"http://www.cuo.so/","m game","zui h",WeChat.WXSceneTimeline));
			}
			if(label=="sendImage"){
				var url:String="http://www.ptbus.com/uploads/allimg/1312/25/1456_1046577301.jpg";
				url=this.saveInfoToCameraRoll(333,"3355");
				trace("file",url);
				trace(wx.sendImageMessage(url,new Date().toString(),WeChat.WXSceneTimeline));
			}
		}
		private function saveInfoToCameraRoll(id:int,pass:String):String
		{
			//			if(CameraRoll.supportsAddBitmapData){
			var bitmap:BitmapData=new BitmapData(480,320);
			var format:TextFormat=new TextFormat();
			format.size=36;
			var title:TextField=new TextField();
			title.defaultTextFormat=format;
			title.width=480;
			title.text="hha";//L.forKey("gametitle");
			bitmap.draw(title);
			var nameLabel:TextField=new TextField();
			nameLabel.defaultTextFormat=format;
			nameLabel.text="ID:"+id;
			nameLabel.width=480;
			var matrix:Matrix=new Matrix();
			matrix.ty=60;
			bitmap.draw(nameLabel,matrix);
			var passLabel:TextField=new TextField();
			passLabel.defaultTextFormat=format;
			passLabel.text="pp";//L.forKey("passwordTag")+pass;
			passLabel.width=480;
			matrix.ty=120;
			bitmap.draw(passLabel,matrix);
			//				var encode:b
			var bytes:ByteArray=bitmap.encode(bitmap.rect,new  PNGEncoderOptions());
			var file:File=File.applicationStorageDirectory.resolvePath("temp.png");
			var st:FileStream=new FileStream();
			st.open(file,FileMode.WRITE);
			st.writeBytes(bytes);
			st.close();
			return file.nativePath;
		}
	}
}